package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
@Slf4j
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增会员
    public void add(Member member) {
        memberDao.add(member);
    }



    @Override
    public Map<String, Object> getMemberReport(String starttime,String endtime) {
        //将开始日期和截至日期类型转换
        Date dBegin = DateUtils.parseString2Date(starttime);
        Date dEnd = DateUtils.parseString2Date(endtime);
        //创建日期集合
        ArrayList<String> months = new ArrayList<>();
        LinkedHashSet<String> set = new LinkedHashSet<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        set.add(sdf.format(dBegin));
        Calendar calBegin = Calendar.getInstance();
        Calendar calEnd = Calendar.getInstance();
        //使用给定时间设置此calender时间
        calBegin.setTime(dBegin);
        calEnd.setTime(dEnd);
        //测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())){
            calBegin.add(Calendar.DAY_OF_MONTH,1);
            set.add(sdf.format(calBegin.getTime()));
        }
        for (String s : set) {
            months.add(s);
        }
        log.debug("service中months集合："+months);
        //创建存储每月会员人数的集合
        List<Long> memberCount = new ArrayList<>();
        //获取每个月对应的会员总数
        for (String month : months) {
            month += "-01";
            String thisMonthLastDay = DateUtils.parseDate2String(DateUtils.getThisMonthLastDay(month));
            long count = memberDao.getMemberCountByMonth(thisMonthLastDay);
            memberCount.add(count);
        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("months", months);
        resultMap.put("memberCount", memberCount);
        log.debug("service实体类方法："+resultMap);
        return resultMap;

        /*List<Map<String,Object>> map = memberDao.getMemberCountByMonth(starttime, endtime);
        log.debug("service方法："+map);
        return map;*/
    }

    @Override
    public List<Map<String, String>> getMemberReportBySex() {
        return memberDao.getMemberCountBySex();
    }

    @Override
    public List<Map<String, String>> getMemberCountByAge() {
        //出生日期集合
        List<String> dates = new ArrayList<>();
        //数据存储
        List<Map<String, String>> list = new ArrayList<>();
        dates.add("1970-01-01");
        dates.add("1995-01-01");
        dates.add("2005-01-01");
        dates.add("2020-01-01");
        for (int i = 0; i < dates.size() - 1; i++) {
            Map<String,String > map = new HashMap<>();

            String date1 = dates.get(i);
            String date2 = dates.get(i + 1);
            Map<String,Object> map1 = memberDao.getMemberCountByAge(date1, date2);
            Object age1 = map1.get("age1");
            Object age2 = map1.get("age2");
            Object value = map1.get("value");
            map.put("value", String.valueOf(value));
            map.put("name",age2+"-"+age1);
            list.add(map);
        }

         return list;
    }
}
